package user;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import user.Title;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class myjdbc {
	public List readFirstTitle(){
		        List<Title> list =new ArrayList<Title>();
	            Connection con=null;
	            PreparedStatement psmt=null;
		        ResultSet rs=null;
		        try {
		             Class.forName("com.mysql.jdbc.Driver");
		         } catch (ClassNotFoundException e) {
		             e.printStackTrace();
		         }
		         
		         try {
		             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/nytest","root","x5");
		             String sql="select * from library";
		             psmt=con.prepareStatement(sql);
		             rs=psmt.executeQuery();
		             
		             while(rs.next())
		             {int a=rs.getInt("xuhao");
		         	String b=rs.getString("bookname");
		        	String c=rs.getString("zuozhe");
		        	int e=rs.getInt("price");
		            Title tl=new Title(a,b,c,e);
		            list.add(tl);
		             }
		         } catch (SQLException e) {
		               e.printStackTrace();
		             }finally
		          {
		        	           try {
		        	               if(rs!=null)
		                       {
		        	                   rs.close();
		        	                 }
		        	                 if(psmt!=null)
		        	                 {
		        	                      psmt.close();
		        	                }
		        	                 if(con!=null)
		        	                 {
		        	                     con.close();
		        	                 }
		        	            } catch (SQLException e) {
		        	                e.printStackTrace();
		        	             }
		        	        }
		        	          return list;
	}
}
